﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Ria;
using AdminApp.Web;

namespace TheOliver.DigitalSignage.Client.Interfaces
{
    public static class Configuration
    {
        private static IsolatedStorageSettings _settings = IsolatedStorageSettings.ApplicationSettings;

        #region Save and Read Settings Method

        private static void SaveSetting(string name, object value)
        {
            if (_settings.Contains(name.ToLower()))
            {
                _settings[name.ToLower()] = value;
            }
            else
            {
                _settings.Add(name.ToLower(), value);
            }
            try
            {
                _settings.Save();
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.Message);
            }
        }

        private static object LoadSetting(string name)
        {
            if (_settings.Contains(name.ToLower()))
            {
                return _settings[name.ToLower()];
            }
            return null; 
        }

        private static object LoadSetting(string name, object defaultValue)
        {
            var result = LoadSetting(name);
            if (result != null)
            {
                return result;
            }
            return defaultValue;
        }

        #endregion

        public static int DefaultSlotLength
        {
            get { return 30; }
        }

        public static DateTime? SimulateDateTime
        {
            get 
            {
                if (LoadSetting("SimulateDateTime") != null)
                {
                    var dt = (DateTime)LoadSetting("SimulateDateTime");
                    DateTime? dth;
                    dth = dt;
                    return dth;
                }
                return null;
            }
            set 
            {
                DateTime? dt = (DateTime?)value;
                if (dt.HasValue)
                {
                    SaveSetting("SimulateDateTime", dt.Value);
                }
            }
        }

        public static EntitySet<AdminApp.Web.AllSlots> AllSlots
        {
            get 
            {
                return LoadSetting("AllSlots") as EntitySet<AdminApp.Web.AllSlots>;
            }
            set 
            {
                SaveSetting("AllSlots", value);
            }
        }

        public static IEnumerable<AdminApp.Web.AllSlots> TodaysUpcomingSessions
        {
            get
            {
                //List<AllSlots> result = new List<AllSlots>();
                //foreach (var s in Configuration.AllSlotsByRoom)
                //{
                //    TimeSpan ts = new TimeSpan(0, s.SlotLength.HasValue ? s.SlotLength.Value : 0, 0);
                //    if (s.SlotDate == Configuration.CurrentDate)
                //    {
                //        if (s.SlotTime == Configuration.CurrentTime)
                //        {
                //            result.Add(s);
                //        }
                //        else if (s.SlotTime >= Configuration.CurrentTime.Subtract(ts))
                //        {
                //            result.Add(s);
                //        }

                //    }
                //}

                var result = Configuration.AllSlotsByRoom
                    .Where(s => s.SlotDate == Configuration.CurrentDate)
                    .Where(s => s.SlotTime >= Configuration.CurrentTime
                    ||
                    s.SlotTime >= Configuration.CurrentTime.Subtract(new TimeSpan(0, (s.SlotLength.HasValue ? s.SlotLength.Value : 0), 0))
                        )
                    .OrderBy(s => s.SlotTime);

                return result;
            }
        }

        public static IEnumerable<AdminApp.Web.AllSlots> AllSlotsByRoom
        {
            get
            {
                var allSlots = LoadSetting("AllSlots") as EntitySet<AdminApp.Web.AllSlots>;
                if (AllSlots == null)
                {
                    return null;
                }

                var result = AllSlots
                    .Where(s => s.RoomId == Configuration.SelectedRoom.Id );

                return result;
            }
        }

        public static DateTime CurrentDateTime
        {
            get
            {
                if (Configuration.SimulateDateTime != null && 
                    Configuration.SimulateDateTime.HasValue)
                {
                    return Configuration.SimulateDateTime.Value;
                }
                return DateTime.Now;
            }
        }

        public static DateTime CurrentDate
        {
            get
            {
                if (Configuration.SimulateDateTime != null)
                {
                    return Configuration.SimulateDateTime.Value.Date;
                }
                return DateTime.Today;
            }
        }

        public static TimeSpan CurrentTime
        {
            get
            {
                if (Configuration.SimulateDateTime != null)
                {
                    return Configuration.SimulateDateTime.Value.TimeOfDay;
                }
                return DateTime.Now.TimeOfDay;
            }
        }

        public static AdminApp.Web.Event SelectedEvent
        {
            get 
            {
                var v = LoadSetting("SelectedEvent");
                if (v == null)
                {
                    return null;
                }
                return (AdminApp.Web.Event)v;
            }
            set 
            {
                SaveSetting("SelectedEvent", value);
            }
        }

        public static AdminApp.Web.Room SelectedRoom
        {
            get
            {
                var v = LoadSetting("SelectedRoom");
                if (v == null)
                {
                    return null;
                }
                return (AdminApp.Web.Room)v;
            }
            set
            {
                SaveSetting("SelectedRoom", value);
            }
        }

        public static EntitySet<AdminApp.Web.Event> Events 
        {
            get
            {
                return LoadSetting("Events") as EntitySet<AdminApp.Web.Event>;
            }
            set
            {
                SaveSetting("Events", value);
            }
        }

        public static AdminApp.Web.AllSlots NextSessionOfToday
        {
            get
            {
                var allSessions = Configuration.AllSlotsByRoom;
                if (allSessions == null)
                {
                    return null;
                }

                var todaySession = allSessions.Where(s => s.SlotDate == Configuration.CurrentDate);
                if (todaySession == null)
                {
                    return null;
                }

                var upcomingSessions = todaySession.Where(s => s.SlotTime > Configuration.CurrentTime)
                    .OrderBy(s => s.SlotTime);
                if (upcomingSessions == null || upcomingSessions.Count() == 0)
                {
                    return null;
                }

                return upcomingSessions.First();
            }
        }

        public static AdminApp.Web.AllSlots CurrentSession
        {
            get
            {
                var allSessions = Configuration.AllSlotsByRoom;
                if (allSessions == null)
                {
                    return null;
                }

                var todaySession = allSessions.Where(s => s.SlotDate == Configuration.CurrentDate);
                if (todaySession == null)
                {
                    return null;
                }

                var currentSessions = todaySession
                    .Where(s => s.SlotTime <= Configuration.CurrentTime 
                        && s.SlotTime.Add(new TimeSpan(0, s.SlotLength.HasValue ? s.SlotLength.Value : Configuration.DefaultSlotLength, 0)) >= Configuration.CurrentTime)
                    .OrderBy(s => s.SlotTime);
                if (currentSessions == null || currentSessions.Count() == 0)
                {
                    return null;
                }

                return currentSessions.First();
            }
        }

        public static int DefaultDisplayDuration 
        {
            get
            {
                var v = LoadSetting("DefaultDisplayDuration", 10);
                return (int)v;
            }
            set
            {
                SaveSetting("DefaultDisplayDuration", value);
            }
        }

        public static int AgendaItemDefaultDisplayTimeInMilliSeconds
        {
            get
            {
                var v = LoadSetting("AgendaItemDefaultDisplayTimeInMilliSeconds", 200);
                return (int)v;
            }
            set
            {
                SaveSetting("AgendaItemDefaultDisplayTimeInMilliSeconds", value);
            }
        }

        public static int AgendaItemDefaultWaitTimeInMilliseconds
        {
            get
            {
                var v = LoadSetting("AgendaItemDefaultWaitTimeInMilliseconds", 7000);
                return (int)v;
            }
            set
            {
                SaveSetting("AgendaItemDefaultWaitTimeInMilliseconds", value);
            }
        }

        public static int MinAgendaItemGap 
        {
            get
            {
                var v = LoadSetting("MinAgendaItemGap", 10);
                return (int)v;
            }
            set
            {
                SaveSetting("MinAgendaItemGap", value);
            }
        }

        public static int DataUpdateInterval
        {
            get
            {
                var v = LoadSetting("DataUpdateInterval", 10);
                return (int)v;
            }
            set
            {
                SaveSetting("DataUpdateInterval", value);
            }
        }
    }
}
